/**
 * @version		$Id: popups.js 53 2011-02-09 10:29:43Z happy_noodle_boy $
 * @package      JCE
 * @copyright    Copyright (C) 2005 - 2009 Ryan Demmer. All rights reserved.
 * @author		Ryan Demmer
 * @license      GNU/GPL
 * JCE is free software. This version may have been modified pursuant
 * to the GNU General Public License, and as distributed it includes or
 * is derivative of works licensed under the GNU General Public License or
 * other free or open source software licenses.
 */

var WFPopups = WFExtensions.add('Popups', {
    /**
     * Array of popup extension objects
     */
    popups : {},

    /**
     * Current Popup Extension object
     */
    popup : '',

    /**
     * Global Configuration object
     */
    config : {},

    /**
     * Add a popup extension
     * @param {String} n Extension name
     * @param {Object} o Extension object
     */
    addPopup : function(n, o) {
        this.popups[n] = o;

        WFExtensions.addExtension('popups', n, o);
    },

    /**
     * Get all popups
     */
    getPopups : function() {
        return this.popups;
    },

    /**
     * Check if selected node is a Popup
     * @param {Object} n Node
     * @param {Object} v Popup type
     */
    isPopup : function(n, v) {
        return n.nodeName == 'A' && this.callFunction('check', n, v);
    },

    /**
     * Get the assigned popup if any from the selected node
     * @param {Object} n Anchor Element / Node
     */
    getPopup : function(n) {
        var t = this, popup, popups = this.getPopups();

        tinymce.each(this.popups, function(o, v) {
            if (t.isPopup(n, v)) {
                t.popup = v;
            }
        });

        if (this.popup) {
            // Select popup in list
            this.selectPopup(this.popup);
            // Check Enable box
            document.getElementById('popup_check').checked = true;
            // Enable Popup List
            document.getElementById('popup_list').disabled = false;
            // Process attributes
            this.getAttributes(n);
        }
    },

    /**
     * Set the currently selected popup
     * @param {String} s popup name eg: jcemediabox
     */
    setPopup : function(s) {
        this.popup = s;
    },

    /**
     * Set Global Configuration
     * @param {Object} config Configuration object
     */
    setConfig : function(config) {
        tinymce.extend(this.config, config);
    },

    /**
     * Set parameters for a popup type
     * @param {String} n Popup type
     * @param {Object} p Parameters object
     */
    setParams : function(n, p) {
        var popup = this.popups[n];

        if (popup) {
            if (typeof popup['params'] == 'undefined') {
                popup['params'] = {};
            }
            tinymce.extend(popup['params'], p);
        }
    },

    /**
     * Get parameters for a popup type
     * @param {String} n Popup Type
     */
    getParams : function(n) {
        return this.popups[n].params || {};
    },

    /**
     * Get a specific popup type parameter
     * @param {String} n Popup type
     * @param {String} p Paremeter
     */
    getParam : function(n, p) {
        var params = this.getParams(n);
        return params[p] || null;
    },

    /**
     * Enable popups after checkbox click
     * @param {Object} el Checkbox element
     */
    enablePopups : function(el) {
        document.getElementById('popup_list').disabled = !el.checked;
    },

    /**
     * Disable popup funcitons
     */
    disablePopups : function() {
        var DOM = tinyMCEPopup.dom;

        DOM.get('popup_list').disabled 	= true;
        DOM.get('popup_check').disabled = true;
        DOM.get('popup_check').checked 	= false;
    },

    /**
     * Selects a popup from the popup list
     * @param {Object} s Select element
     */
    selectPopup : function(v) {
        var o, i, DOM = tinyMCEPopup.dom, s = DOM.get('popup_list');

        for (i = 0; i < s.options.length; i++) {
            o = s.options[i];
            if (o.value) {
                // hide all popups
                DOM.hide('popup_extension_' + o.value);
                if (v == o.value || o.selected) {
                    o.selected = true;

                    DOM.show('popup_extension_' + o.value);
                    // set as selected popup
                    this.popup = o.value;
                    // call onSelect function
                    this.callFunction('onSelect', [], o.value);
                }
            }
        }
    },

    /**
     * Set popup extension parameter values to current node
     * @param {Object} n Popup / Link node
     */
    setAttributes : function(n, args) {
        var DOM = tinyMCEPopup.dom;

        // map values
        if (this.config['map']) {
            tinymce.each(this.config['map'], function (k, at) {
                n[at] = args[k] || DOM.get(k).value;
            });

        }

        return this.callFunction('setAttributes', [n, args]);
    },

    /**
     * Apply currently selected popup attributes to link element
     * @param {Object} n Link element / node
     */
    getAttributes : function(n) {
        var DOM = tinyMCEPopup.dom, ed = tinyMCEPopup.editor, k, v, at, data;

        if (n.nodeName != 'A') {
            n = ed.dom.getParent(n, 'a');
        }

        if (this.isPopup(n)) {
            data = this.callFunction('getAttributes', n);
        }

        return data;
    },

    /**
     * Call a popup function
     * @param {String} fn Function to call
     * @param {Array} args Array of arguments
     * @param {String} popup popup type to call function on
     */
    callFunction : function(fn, args, popup) {
        if (!popup) {
            popup = this.popup;
        }

        if (popup) {
            return WFExtensions.callFunction('popups', popup, fn, args);
        }

        return false;
    },

    /**
     * Check if popups are enabled (checkbox checked and popup type selected)
     */
    isEnabled : function() {
        var DOM = tinyMCEPopup.dom;

        return DOM.get('popup_check').checked && this.popup;
    },

    /**
     * Create a popup on a link element
     * @param {Object} n
     * @param {Object} args
     */
    createPopup : function(n, args) {
        var self = this, ed = tinyMCEPopup.editor, DOM = tinyMCEPopup.dom, o;

        args = args || {};

        // Popup option is enabled
        if (this.isEnabled()) {

            if (n && (n.nodeName == 'A' || (n = ed.dom.getParent(n, 'A')))) {
                // remove all popups
                this.removePopups(n);
                // set popup attributes
                this.setAttributes(n, args);
            } else {
                // no selection
                if (ed.selection.isCollapsed()) {
                    ed.execCommand('mceInsertContent', false, '<a href="javascript:mctmp(0);">' + DOM.get('popup_text').value + '</a>', {skip_undo : 1});
                    // create link on selection or update existing link
                } else {
                    ed.execCommand('mceInsertLink', false, 'javascript:mctmp(0);');
                }

                tinymce.each(ed.dom.select('a[href=javascript:mctmp(0);]'), function(link) {
                    self.setAttributes(link, args);
                });

                // move selection
                ed.selection.collapse();
            }
        } else {
            // is a popup and option not checked - remove
            if (this.isPopup(n)) {
                ed.dom.remove(n, true);
            }
        }
    },

    /**
     * Remove all popups from the current link node
     */
    removePopups : function(n) {
        var t = this;
        tinymce.each(this.popups, function(o, v) {
            t.callFunction('remove', n, v);
        });

    },

    /**
     * Function called when a file is selected
     * @param {Object} args Arguments object eg: {src : url, width : 640, height : 480}
     */
    onSelectFile : function(args) {
        this.callFunction('onSelectFile', args);
    }

});
